from src.Business.wechat.WxPayTool import WxPay
from src.api.getToken import ConfigParser

APP_ID = "wx3588c79e5588d6bd"
APP_SECRET = "cac006c758deea10927014fc7b290445"
MCH_ID = "1650718617"


class PayAgent:
    wxpay = None

    def __init__(self):
        con = ConfigParser()
        app_id = con.get_appid()
        app_secret = con.get_appsecret()
        mch_id = con.get_mch_id()
        self.wxpay = WxPay(APP_ID=app_id, APP_SECRET=app_secret, MCH_ID=mch_id)

    def get_pay_info(self):
        return {
            "body": "支付测试",
            "detail": "入住",
            "attach": "河南彭于晏",
            "total_fee": 1,  # 总金额
            # 终端IP(用户ip) F
            "spbill_create_ip": "192.168.8.3",
            # # 支付成功之后，微信异步通知(post) T
            # "notify_url": "http://121.36.196.239:1000/applet/payNotify",
            "notify_url": "http://mini.unicorn-go.com:1000/applet/payNotify",
        }

    def pay(self, code, pay_info):
        """
        支付
        """
        wexin_pay = self.wxpay
        result, message = wexin_pay.get_open_id(code)
        if result:
            prepay_dict, prepay_id, info_dict, info = wexin_pay.pay(**pay_info)
            print("prepay_dict: ", prepay_dict)
            print("prepay_id: ", prepay_id)
            print("info_dict: ", info_dict)
            print("info: ", info)
            return (prepay_dict, prepay_id, info_dict, info)
        else:
            raise ValueError(message)

    def pay_test(self, code):
        """
        支付测试
        """
        return self.pay(code, self.get_pay_info())


class PayAgent2:
    wxpay = None

    def __init__(self):
        con = ConfigParser()
        app_id = con.get_appid()
        app_secret = con.get_appsecret()
        mch_id = con.get_mch_id()
        # app_id = "wx3588c79e5588d6bd"
        # app_secret = "cac006c758deea10927014fc7b290445"
        # mch_id = "1650718617"
        self.wxpay = WxPay(APP_ID=app_id, APP_SECRET=app_secret, MCH_ID=mch_id)

    def get_pay_info(self):
        return {
            "body": "支付测试",
            "detail": "入住",
            "attach": "河南彭于晏",
            "total_fee": 1,  # 总金额
            # 终端IP(用户ip) F
            "spbill_create_ip": "192.168.8.3",
            # # 支付成功之后，微信异步通知(post) T
            # "notify_url": "http://121.36.196.239:1000/applet/payNotify",
            "notify_url": "http://mini.unicorn-go.com:1000/applet/payNotify",
        }

    def pay(self, code, pay_info):
        """
        支付
        """
        wexin_pay = self.wxpay
        result, message = wexin_pay.get_open_id(code)
        if result:
            prepay_dict, prepay_id, info_dict, info = wexin_pay.pay(**pay_info)
            return (prepay_dict, prepay_id, info_dict, info)
        else:
            raise ValueError(message)

    def refund(self, **kwargs):
        """
        退款
        """
        wexin_pay = self.wxpay
        out_trade_no = kwargs.get("out_trade_no")
        total_fee = kwargs.get("total_fee")
        refund_fee = kwargs.get("refund_fee")
        if out_trade_no is None:
            raise ValueError("请输入退款订单号 out_trade_no")
        if total_fee is None:
            raise ValueError("请输入订单总金额 total_fee")
        if refund_fee is None:
            raise ValueError("请输入退款金额 refund_fee")
        return wexin_pay.refund(**kwargs)

    def pay_test(self, code):
        """
        支付测试
        """
        return self.pay(code, self.get_pay_info())
